/**
 * @file dhcp.h
 * @brief Defines types, constants and functions that are specific to a DHCP 
 *        protocol implementation.
 * @author Vamshi Ponnapalli
 */

#ifndef _DHCP_H
  #define _DHCP_H
  /*****************************************************************************
   * Includes
   ****************************************************************************/
  #include "hal_types.h"
  #include "hal_defs.h"
  #include "network.h"

  #ifdef __cplusplus
    extern "C" {
  #endif
  /*****************************************************************************
   * Types
   ****************************************************************************/   
  /**
   * Represents a DHCP message.
   */
  typedef struct 
  {
    uint8 op;             // message type - 1 = request, 2 = reply
    uint8 htype;          // hardware address type - 1 = 10mb Ethernet
    uint8 hlen;           // hardware address length
    uint8 hops;           // (something about relay agents?)
    uint32 xid;           // transaction ID
    uint16 secs;          // seconds since process started, set by client
    uint16 flags;         // flags to set options
    uint32 ciaddr;        // client's IP address, used when it already has an IP
    uint32 yiaddr;        // client's IP address
    uint32 siaddr;        // IP address of next server for bootstrap process
    uint32 giaddr;        // relay agent IP address (?)
    uint8 chaddr[16];     // client's hardware address
    uint8 sname[64];      // server host name
    uint8 filename[128];  // name of boot file
    uint8 options[340];   // variable-length options list
  } dhcp_message_t;
  
  /**
   * Represents information regarding the position of a specific DHCP option
   * within a DHCP message.
   */
  typedef struct
  {
    uint8 *pOption; //Pointer to the option location.
    uint8 size;     //Size of the option in bytes.
  }
  dhcp_option_value_t;
  
  /*****************************************************************************
  * Constants
  *****************************************************************************/ 
  //Size of the DHCP header
  #define DHCP_HDR_SIZE           236
  
  //Max size of an entire DHCP message
  #define MAX_DHCP_MSG_SIZE       576
  
  //Max size of options field we can receive
  #define MAX_DHCP_OPT_SIZE       340
  
  //Longest timeout (in sec) allowed between sending DHCP requests
  #define MAX_DHCP_TIMEOUT	  16
  
  //Starting value for timeout (in sec)
  #define START_DHCP_TIMEOUT	  8
  
  //DHCP options
  #define DHCP_OPT_SUBNET         1
  #define DHCP_OPT_ROUTERS        3
  #define DHCP_OPT_DNS            6
  #define DHCP_OPT_HOSTNAME       12
  #define DHCP_OPT_REQIP          50
  #define DHCP_OPT_LEASETIME      51
  #define DHCP_OPT_OVERLOAD       52
  #define DHCP_OPT_TYPE           53
  #define DHCP_OPT_SERVERID       54
  #define DHCP_OPT_REQPARAMS      55
  #define DHCP_OPT_MAXSIZE	  57
  #define DHCP_OPT_CLIENTID       61
  #define DHCP_OPT_END		  255
  #define DHCP_OPT_PAD		  0
  
  
  //DHCP message types
  #define DHCPDISCOVER	          1
  #define DHCPOFFER		  2
  #define DHCPREQUEST             3
  #define DHCPDECLINE             4
  #define DHCPACK		  5
  #define DHCPNAK		  6
  #define DHCPRELEASE             7

  #ifdef __cplusplus
    }
  #endif

#endif
